import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsProvider with ChangeNotifier {
  late SharedPreferences _prefs;
  bool _isInitialized = false;

  // 设置项
  String _deviceName = '我的补光灯';
  int _autoOffMinutes = 30;
  ThemeMode _themeMode = ThemeMode.system;
  bool _useTransition = true;
  
  // 新增高级设置项
  double _transitionDuration = 300; // 渐变持续时间（毫秒）
  double _minBrightness = 0.0; // 最小亮度
  double _maxBrightness = 1.0; // 最大亮度
  double _minColorTemp = 0.0; // 最小色温
  double _maxColorTemp = 1.0; // 最大色温
  bool _rememberLastSettings = true; // 记住上次的设置
  bool _doubleClickToMax = true; // 双击开关达到最大亮度
  String _startupMode = 'last'; // 启动模式：last/custom/off
  double _startupBrightness = 0.5; // 自定义启动亮度
  double _startupColorTemp = 0.5; // 自定义启动色温

  // Getters
  String get deviceName => _deviceName;
  int get autoOffMinutes => _autoOffMinutes;
  ThemeMode get themeMode => _themeMode;
  bool get useTransition => _useTransition;
  double get transitionDuration => _transitionDuration;
  double get minBrightness => _minBrightness;
  double get maxBrightness => _maxBrightness;
  double get minColorTemp => _minColorTemp;
  double get maxColorTemp => _maxColorTemp;
  bool get rememberLastSettings => _rememberLastSettings;
  bool get doubleClickToMax => _doubleClickToMax;
  String get startupMode => _startupMode;
  double get startupBrightness => _startupBrightness;
  double get startupColorTemp => _startupColorTemp;

  // 初始化
  Future<void> init() async {
    if (_isInitialized) return;
    _prefs = await SharedPreferences.getInstance();
    
    // 加载基本设置
    _deviceName = _prefs.getString('deviceName') ?? _deviceName;
    _autoOffMinutes = _prefs.getInt('autoOffMinutes') ?? _autoOffMinutes;
    _themeMode = ThemeMode.values[_prefs.getInt('themeMode') ?? ThemeMode.system.index];
    _useTransition = _prefs.getBool('useTransition') ?? _useTransition;
    
    // 加载高级设置
    _transitionDuration = _prefs.getDouble('transitionDuration') ?? _transitionDuration;
    _minBrightness = _prefs.getDouble('minBrightness') ?? _minBrightness;
    _maxBrightness = _prefs.getDouble('maxBrightness') ?? _maxBrightness;
    _minColorTemp = _prefs.getDouble('minColorTemp') ?? _minColorTemp;
    _maxColorTemp = _prefs.getDouble('maxColorTemp') ?? _maxColorTemp;
    _rememberLastSettings = _prefs.getBool('rememberLastSettings') ?? _rememberLastSettings;
    _doubleClickToMax = _prefs.getBool('doubleClickToMax') ?? _doubleClickToMax;
    _startupMode = _prefs.getString('startupMode') ?? _startupMode;
    _startupBrightness = _prefs.getDouble('startupBrightness') ?? _startupBrightness;
    _startupColorTemp = _prefs.getDouble('startupColorTemp') ?? _startupColorTemp;
    
    _isInitialized = true;
    notifyListeners();
  }

  // 基本设置 Setters
  Future<void> setDeviceName(String name) async {
    _deviceName = name;
    await _prefs.setString('deviceName', name);
    notifyListeners();
  }

  Future<void> setAutoOffMinutes(int minutes) async {
    _autoOffMinutes = minutes;
    await _prefs.setInt('autoOffMinutes', minutes);
    notifyListeners();
  }

  Future<void> setThemeMode(ThemeMode mode) async {
    _themeMode = mode;
    await _prefs.setInt('themeMode', mode.index);
    notifyListeners();
  }

  Future<void> setUseTransition(bool value) async {
    _useTransition = value;
    await _prefs.setBool('useTransition', value);
    notifyListeners();
  }

  // 高级设置 Setters
  Future<void> setTransitionDuration(double duration) async {
    _transitionDuration = duration;
    await _prefs.setDouble('transitionDuration', duration);
    notifyListeners();
  }

  Future<void> setBrightnessRange(double min, double max) async {
    _minBrightness = min;
    _maxBrightness = max;
    await _prefs.setDouble('minBrightness', min);
    await _prefs.setDouble('maxBrightness', max);
    notifyListeners();
  }

  Future<void> setColorTempRange(double min, double max) async {
    _minColorTemp = min;
    _maxColorTemp = max;
    await _prefs.setDouble('minColorTemp', min);
    await _prefs.setDouble('maxColorTemp', max);
    notifyListeners();
  }

  Future<void> setRememberLastSettings(bool value) async {
    _rememberLastSettings = value;
    await _prefs.setBool('rememberLastSettings', value);
    notifyListeners();
  }

  Future<void> setDoubleClickToMax(bool value) async {
    _doubleClickToMax = value;
    await _prefs.setBool('doubleClickToMax', value);
    notifyListeners();
  }

  Future<void> setStartupSettings({
    String? mode,
    double? brightness,
    double? colorTemp,
  }) async {
    if (mode != null) {
      _startupMode = mode;
      await _prefs.setString('startupMode', mode);
    }
    if (brightness != null) {
      _startupBrightness = brightness;
      await _prefs.setDouble('startupBrightness', brightness);
    }
    if (colorTemp != null) {
      _startupColorTemp = colorTemp;
      await _prefs.setDouble('startupColorTemp', colorTemp);
    }
    notifyListeners();
  }
}
